package cn.gogpay.duoim.sdk.service.v3;

import cn.gogpay.duoim.sdk.model.IMRespResult;
import cn.gogpay.duoim.sdk.model.v3.base.IMCertificate;
import cn.gogpay.duoim.sdk.model.v3.user.req.*;
import cn.gogpay.duoim.sdk.model.v3.user.resp.GetDesignateUsersResp;
import cn.gogpay.duoim.sdk.model.v3.user.resp.GetUserStatusResp;
import cn.gogpay.duoim.sdk.model.v3.user.resp.GetUsersOnlineStatusRespSuccessResult;
import cn.gogpay.duoim.sdk.service.IMApiFactory;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import okhttp3.Request;

public class IMUserV3Service {

    /**
     * 一般是通过业务服务器（如chat服务器）完成用户的账号注册后，再注册到IM系统中，这样业务系统和IM系统完成账号打通。
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> userRegister(IMCertificate token, UserRegisterReq req) {
        req.setSecret(IMApiFactory.buildSecret());
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/user/user_register");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 更新用户在IM中的资料，主要是头像、昵称
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> updateUserInfo(IMCertificate token, UpdateUserInfoReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/user/update_user_info");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 用户信息查询
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetDesignateUsersResp> getUsersPublicInfo(IMCertificate token, GetDesignateUsersReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/user/get_users_info");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetDesignateUsersResp>> typeReference = new TypeReference<IMRespResult<GetDesignateUsersResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 获取用户状态
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetUserStatusResp> getUsersStatus(IMCertificate token, GetUserStatusReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/user/get_users_status");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetUserStatusResp>> typeReference = new TypeReference<IMRespResult<GetUserStatusResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 获取用户在线状态
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetUsersOnlineStatusRespSuccessResult> getUsersOnlineStatus(IMCertificate token, GetUsersOnlineStatusReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/user/get_users_online_status");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetUsersOnlineStatusRespSuccessResult>> typeReference = new TypeReference<IMRespResult<GetUsersOnlineStatusRespSuccessResult>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }
}
